<template>
  <div>
    <base-list ref="baselist"
               :condition="condition"
               :datas.sync="cargoData"
               :pageSearch="pageSearch">
      <el-row slot="search"
              class="title-header">
        <h4>转存单处理页面</h4>
      </el-row>
      <el-form :inline="true"
               :model="condition"
               class="search-form"
               slot="search"
               ref="aList"
               label-width="100px">
        <el-row>
          <el-col :span="5"
                  class="search-width6">
            <el-form-item label="货主">
              <auto-search-name placeholder="请选择"
                                @selectId="v=>condition['search_EQ_cargoOwnerId']=v"
                                :name="form.cargoOwnerName"></auto-search-name>
            </el-form-item>
          </el-col>
          <el-col :span="8"
                  class="search-width6">
            <el-form-item label="车船号">
              <el-input v-model="condition['search_LIKE_transhipNo']"
                        placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width6">
            <el-form-item label="品名">
              <el-select v-model="condition['search_EQ_cargoCode']"
                         placeholder="请选择"
                         clearable
                         filterable>
                <el-option v-for="item in cargoNameList"
                           :key="item.cargoCode"
                           :label="item.cargoName"
                           :value="item.cargoCode"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width6">
            <el-form-item label="材质">
              <auto-search-dic v-model="condition['search_EQ_material']"
                               @selectName="v=>condition['search_EQ_material']=v"
                               code="CARGO_CZ"></auto-search-dic>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width6">
            <el-form-item label="生产厂家"
                          clearable>
              <auto-search-dic v-model="condition['search_EQ_madeBy']"
                               @selectName="v=>condition['search_EQ_madeBy']=v"
                               code="CARGO_SCZ"></auto-search-dic>
            </el-form-item>
          </el-col>
          <el-col :span="24"
                  class="search-width6">
            <el-form-item label="管理方式">
              <el-select v-model="condition['search_EQ_manageWay']"
                         placeholder="请选择"
                         clearable
                         filterable>
                <el-option v-for="[key, val] in _dictManagement"
                           :key="key"
                           :label="val"
                           :value="key"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24"
                  class="search-width6">
            <el-form-item label="计量方式">
              <el-select v-model="condition['search_EQ_measureWay']"
                         placeholder="请选择"
                         clearable
                         filterable>
                <el-option v-for="[key, val] in _dictMeterStyle"
                           :key="key"
                           :label="val"
                           :value="key"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width8">
            <el-form-item label="规格"
                          class="gg-input">
              <el-col :span="8">
                <auto-search-dic style="width: 100px;"
                                 @selectName="v=>condition['search_EQ_normOne']=v"
                                 code="CARGO_GGY"></auto-search-dic>
              </el-col>
              <el-col :span="8">
                <auto-search-dic style="width: 100px;"
                                 @selectName="v=>condition['search_EQ_normTwo']=v"
                                 code="CARGO_GGE"></auto-search-dic>
              </el-col>
              <el-col :span="8">
                <auto-search-dic style="width: 100px;"
                                 @selectName="v=>condition['search_EQ_normThree']=v"
                                 code="CARGO_GGS"></auto-search-dic>
              </el-col>
            </el-form-item>
          </el-col>
          <el-col :span="5"
                  class="search-width8">
            <el-form-item label="接收时间">
              <el-date-picker style="width:150px;"
                              v-model="condition['search_GTE_createDateTime']"
                              type="date"
                              placeholder="选择日期"
                              value-format="yyyy-MM-dd 00:00:00"> </el-date-picker> 至
              <el-date-picker style="width:150px;"
                              v-model="condition['search_LTE_createDateTime']"
                              type="date"
                              placeholder="选择日期"
                              value-format="yyyy-MM-dd 23:59:59"> </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-button type="primary"
                       icon="el-icon-search"
                       class="ml-100"
                       @click="loadData">查 询</el-button>
            <el-button formVisible="true"
                       icon="el-icon-refresh"
                       @click="resuleBtn('aList')">
              重置
            </el-button>
            <el-button formVisible="true"
                       icon="el-icon-edit-outline"
                       @click="handleDialogForm">
              转为存单
            </el-button>
          </el-col>
        </el-row>
      </el-form>
      <el-table slot="list"
                ref="multipleTable"
                :data="cargoData"
                border
                stripe
                @selection-change="handleSelectionChange"
                show-summary
                :summary-method="getSummaries"
                max-height="550">
        <el-table-column type="selection"
                         width="50"
                         align="center"></el-table-column>
        <el-table-column type="index"
                         label="序号"
                         width="50"
                         align="center"></el-table-column>
        <el-table-column prop="checkAcceptNo"
                         label="验收单号"
                         min-width="120"></el-table-column>
        <el-table-column prop="transhipNo"
                         label="车船号"
                         min-width="120"></el-table-column>
        <el-table-column prop="auditStatus"
                         label="状态"
                         min-width="100">
          <template slot-scope="scope">
            {{ scope.row.auditStatus | _filterInwayStatus}}
          </template>
        </el-table-column>
        <el-table-column prop="cargoOwnerName"
                         label="货主"
                         min-width="150"></el-table-column>
        <el-table-column prop="cargoName"
                         label="品名"
                         min-width="120"></el-table-column>
        <el-table-column prop="normStr"
                         label="规格"
                         min-width="120"></el-table-column>
        <el-table-column prop="material"
                         label="材质"
                         min-width="100"></el-table-column>
        <el-table-column prop="madeBy"
                         label="生产厂家"
                         min-width="100"></el-table-column>
        <el-table-column prop="measureWay"
                         label="计量方式"
                         min-width="120">
          <template slot-scope="scope">
            {{ scope.row.measureWay | _filterMeterStyle}}
          </template>
        </el-table-column>
        <el-table-column prop="manageWay"
                         label="管理方式"
                         min-width="120">
          <template slot-scope="scope">
            {{ scope.row.manageWay |_filterManagement}}
          </template>
        </el-table-column>
        <el-table-column prop="checkAcceptNum"
                         label="验收件数"
                         min-width="100"></el-table-column>
        <el-table-column prop="checkAcceptWeight"
                         label="验收重量"
                         min-width="100"></el-table-column>
        <el-table-column label="操作"
                         align="center"
                         min-width="200"
                         fixed="right">
          <template slot-scope="scope">
            <el-button type="text"
                       size="small"
                       @click="handleViewDialogForm(scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </base-list>
    <add-deposit-receipt-form ref="addDepositReceiptForm"
                              load="loadData"></add-deposit-receipt-form>
    <view-in-check-detail-form ref="viewInCheckDetailForm"></view-in-check-detail-form>
  </div>
</template>

<script>
import { getAcceptanceInList } from '@/api/enter/acceptanceIn' //表格列表
import { getCargoNameList, getPiecesCargo } from '@/api/things/accountPageList'
import autoSearchDic from '@/components/autocomplete/autoSearchDic'
import autoSearchName from '@/components/autocomplete/autoSearchName' //货主组件
import autoSearchCargoTypeName from '@/components/autocomplete/autoSearchCargoTypeName'
import viewInCheckDetailForm from '../viewInCheckDetailForm'
import addDepositReceiptForm from './addDepositReceiptForm'

export default {
  name: 'depositSlipCom',
  components: {
    autoSearchDic,
    autoSearchName,
    viewInCheckDetailForm,
    addDepositReceiptForm
  },
  data() {
    return {
      form: this.clearForm(),
      pageSearch: getAcceptanceInList, // 分页查询api接口
      condition: { search_EQ_adiutStatus: 'C' },
      component: '',
      warehouseList: [], //仓库
      cargoNameList: [], //货名
      cargoData: []
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.loadData()
    })
    //获取货名
    getCargoNameList(this.$store.state.user.currentUser.org.id).then(res => {
      this.cargoNameList = res.data.aaData
    })
  },
  methods: {
    clearForm() {
      return {}
    },
    loadData() {
      this.$refs.baselist.loadData()
    },
    //转为存单
    handleDialogForm() {
      if (this.multipleSelection.length === 0) {
        this.$message({ type: 'error', message: '请至少选择一条数据' })
      } else {
        let data = this.multipleSelection
        let detailVoList = []
        let ids = []
        for (let item of data) {
          ids.push(item.id)
          let list = item.detailVoList
          for (let e of list) {
            detailVoList.push(e)
          }
        }
        this.$refs.addDepositReceiptForm.form = data[0]
        this.$refs.addDepositReceiptForm.form.ids = ids
        this.$refs.addDepositReceiptForm.form.detailVoList = detailVoList
        this.$refs.addDepositReceiptForm.dialogForm = true
      }
    },
    //查看
    handleViewDialogForm(item) {
      console.log('查看', item)
      this.$refs.viewInCheckDetailForm.form = item
      this.$refs.viewInCheckDetailForm.dialogForm = true
    },
    //合计
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        const values = data.map(item => Number(item[column.property]))
        if (
          column.property === 'totalReleasedWeight' ||
          column.property === 'totalPledgedWeight' ||
          column.property === 'totalControlWeight' ||
          column.property === 'totalUseWeight'
        ) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index]
        } else {
          sums[index] = ''
        }
      })
      return sums
    },
    //预报确认
    confirmForecast(item) {
      this.$confirm(`是否预报确认?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          unfrozenStock(item.id, { reason: '' }).then(res => {
            this.$message({
              type: 'success',
              message: `预报确认成功!`
            })
            this.loadData()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    //取消确认
    consoleForecast(item) {
      this.$confirm(`是否预报确认?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          unfrozenStock(item.id, { reason: '' }).then(res => {
            this.$message({
              type: 'success',
              message: `预报确认成功!`
            })
            this.loadData()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    //删除
    handleRemove(item) {
      this.$confirm(`是否删除该数据?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteCargo(item.id).then(res => {
            this.$message({
              type: 'success',
              message: `删除成功!`
            })
            this.loadData()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    //获取批量删除的数据
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    //重置
    resuleBtn(aList) {
      this.$refs[aList].resetFields()
      this.condition = this.clearForm()
    }
  }
}
</script>
<style scoped>
